class_name BasicCharacterState
extends Resource

signal state_changed
@export var max_health: int = 1

@export var power: int:
	set = set_power
@export var health: int:
	set = set_health
@export var shield: int:
	set = set_shield
@export var act_sprite_frames: SpriteFrames
@export var armor_break: int = 0:
	set = set_armor_break
@export var weak: int = 0:
	set = set_weak
@export var temp_power: int = 0
@export var temp_max_health: int = 0
@export var counter_attack:int = 0:
	set = set_counter_attack
var round_power: int:
	set = set_round_power
var round_shield: int:
	set = set_round_shield
var shield_cumulation = 0:
	set = set_shield_cumulation
@export var unyielding = 0:
	set = set_unyielding
	
func set_unyielding(value):
	unyielding = clampi(value, 0, 999)
	state_changed.emit(StateBar.STATE.UNYIELDING, unyielding)	
	
func set_shield_cumulation(value):
	shield_cumulation = clampi(value, 0, 999)
	state_changed.emit(StateBar.STATE.SHIELD_CUMULATION, shield_cumulation)	
	
func set_counter_attack(value):
	counter_attack = clampi(value, 0, 999)
	state_changed.emit(StateBar.STATE.COUNTER_ATTACK, counter_attack)

func set_power(value: int) -> void:
	power = clampi(value, 0, 999)
	state_changed.emit(StateBar.STATE.POWER, power)
	
func set_round_power(value: int) -> void:
	round_power = clampi(value, 0, 999)
	state_changed.emit(StateBar.STATE.ROUND_POWER, round_power)

func set_round_shield(value: int) -> void:
	round_shield = clampi(value, 0, 999)
	state_changed.emit(StateBar.STATE.ROUND_SHIELD, round_shield)
	
func set_temp_power(value: int, duration: int) -> void:
	temp_power = clampi(value, 0, 999)
	power += temp_power
	state_changed.emit(StateBar.STATE.TEMP_POWER, temp_power, duration)


func set_temp_max_health(value: int, duration: int) -> void:
	temp_max_health = clampi(value, 0, 999)
	max_health += temp_max_health
	state_changed.emit(StateBar.STATE.TEMP_MAX_HEALTH, temp_max_health, duration)


func set_health(value: int) -> void:
	health = clampi(value, 0, max_health)
	state_changed.emit(StateBar.STATE.NOTHING)


func set_shield(value: int) -> void:
	shield = clampi(value, 0, 999)
	state_changed.emit(StateBar.STATE.SHIELD, shield)


func set_weak(value: int) -> void:
	weak = clampi(value, 0, 999)
	state_changed.emit(StateBar.STATE.WEAK, weak, weak)


func set_armor_break(value: int) -> void:
	armor_break = clampi(value, 0, 999)
	state_changed.emit(StateBar.STATE.ARMOR_BREAK, armor_break, armor_break)


func take_damage(damage: int) -> void:
	if damage <= 0:
		return
	var initial_damage = damage
	damage = clampi(damage - shield, 0, damage)
	self.shield = clampi(shield - initial_damage, 0, shield)
	self.health -= damage
	
	
func trial_take_damage(damage: int,times: int) -> int:
	var temp_shield = self.shield 
	for i in times:
		var initial_damage = damage
		damage = clampi(damage - temp_shield, 0, damage)
		temp_shield = clampi(temp_shield - initial_damage, 0, temp_shield)
		if self.health - damage <= 0:
			return i
	return -1

func heal(amount: int) -> void:
	self.health += amount
	state_changed.emit()


func create_instance() -> Resource:
	var instance: BasicCharacterState = self.duplicate()
	instance.health = health
	instance.shield = shield
	return instance


func has_armor_brak_state() -> bool:
	if self.armor_break == 0:
		return false
	else:
		return true


func has_weak_state() -> bool:
	if self.weak == 0:
		return false
	else:
		return true


func has_unyielding_state() -> bool:
	if self.unyielding == 0:
		return false
	else:
		return true
		

func clear_debuff():
	armor_break = 0
	weak = 0

